import json


def open_data():
    global item
    with open('./data.json', 'r', encoding='utf-8') as file:
        res = file.read()

    res_json = json.loads(res)

    item_list = []
    for items_all in res_json['data']['section_nodes']:
        if items_all['title'] == '购物':
            for item in items_all['nodes']:
                # print(item)
                item_list.append(item)

    return item_list


def open_categroies():
    with open('./categories.json', 'r') as file:
        res = file.read()
    res_json = json.loads(res)
    platform = res_json['data']['items']
    datalist = []
    i = 0
    while i < len(platform):
        data_dict = {}
        platform_name = platform[i]['name']
        platform_ID = platform[i]['ID']
        platform_display = platform[i]['display']
        platform_logo = platform[i]['logo']
        platform_homepage = platform[i]['homepage']
        platform_last_time = platform[i]['last_time']
        platform_hashid = platform[i]['hashid']
        platform_fans = platform[i]['fans']
        platform_items = platform[i]['items']
        # print(platform_name, platform_ID, platform_display, platform_logo, platform_homepage, platform_last_time,platform_hashid, platform_fans, platform_items)
        if platform_name == "淘宝 ‧ 天猫":
            data_dict['name'] = platform_display
            data_dict['ID'] = platform_ID
            datalist.append(data_dict)

        i += 1
    print(datalist)


item_list = open_data()
for item_data in item_list:
    platform_name = item_data['name']
    if platform_name == "淘宝 ‧ 天猫":
        continue
    platform_logo = item_data['logo']

    # 分类表数据提取
    category_name = item_data['display']
    category_id = int(item_data['ID'])
    category_link = f'https://api2.tophub.app/nodes/{category_id}'
    print(platform_name,category_link,platform_logo,category_name)

